﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DA;
using System.Text.RegularExpressions;
using Entities;

namespace BL
{
    public class SupplierManager
    {
        public IEnumerable<ERP_Suppliers> GetSuppliers()
        {
            using (dotNetivotEntities model = new dotNetivotEntities())
            {
                return model.ERP_Suppliers.ToList();
            }
        }

        public ERP_Suppliers GetSupplier(int SupplierId)
        {
            using (dotNetivotEntities model = new dotNetivotEntities())
            {
                return model.ERP_Suppliers.SingleOrDefault(x => x.SupplierId == SupplierId);
            }
        }

        public bool EditSupplier(int SupplierId, ERP_Suppliers supplier)
        {
            try
            {
                using (dotNetivotEntities model = new dotNetivotEntities())
                {
                    ERP_Suppliers SupplierToUpdate = model.ERP_Suppliers.SingleOrDefault(x => x.SupplierId == SupplierId);
                    SupplierToUpdate.Address = supplier.Address;
                    SupplierToUpdate.City = supplier.City;
                    SupplierToUpdate.VAT_Number = supplier.VAT_Number;
                    SupplierToUpdate.Name = supplier.Name;
                    SupplierToUpdate.Phone_Number = supplier.Phone_Number;
                    model.SaveChanges();
                    model.AcceptAllChanges();
                }
            }
            catch
            {
                return false;
            }

            return true;
        }

        public bool AddSupplier(ERP_Suppliers Supplier)
        {
            try
            {
                using (dotNetivotEntities model = new dotNetivotEntities())
                {
                    model.ERP_Suppliers.AddObject(Supplier);
                    model.SaveChanges();
                    model.AcceptAllChanges();
                }
            }
            catch
            {
                return false;
            }
            return true;
        }
    }
}
